﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntoTheVoid.Units
{
	using Newtonsoft.Json;
	using Newtonsoft.Json.Linq;
	using System;
	using System.IO;

	public class FileHelper
	{
		private readonly string filePath;

		public FileHelper(string filePath)
		{
			this.filePath = filePath;
		}

		public JObject ReadFile()
		{
			try
			{
				string jsonContent = File.ReadAllText(filePath);
				return JsonConvert.DeserializeObject<JObject>(jsonContent);
			}
			catch (FileNotFoundException)
			{
		//		Console.WriteLine($"File '{filePath}' not found.");
				return null;
			}
			catch (JsonReaderException)
			{
		//		Console.WriteLine($"Unable to parse JSON file '{filePath}'. Please check the file format.");
				return null;
			}
		}

		public void SaveFile(JObject json)
		{
			try
			{
				string jsonString = JsonConvert.SerializeObject(json, Formatting.Indented);
				File.WriteAllText(filePath, jsonString);
	//			Console.WriteLine($"Changes saved to '{filePath}'.");
			}
			catch (IOException)
			{
	//			Console.WriteLine($"Unable to save changes to '{filePath}'.");
			}
		}

	}	
}
